package com.orange.quickback.system.service.user.manage.impl;

import com.orange.quickback.system.dao.user.manage.QBootUserRepository;
import com.orange.quickback.system.entities.user.manage.QBootRole;
import com.orange.quickback.system.entities.user.manage.QBootUser;
import com.orange.quickback.system.entities.user.manage.user.details.QBootUserDetails;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service("UserDetails")
public class QBootUserDetailServiceImpl implements UserDetailsService {
    @Resource
    QBootUserRepository qBootUserRepository;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        QBootUserDetails qBootUserDetails = new QBootUserDetails();
        QBootUser user= qBootUserRepository.findByUserName(username);
        if(user == null){
            throw  new UsernameNotFoundException("账户不存在");
        }
        List<QBootRole> roles = user.getRole_users();
        roles.add(new QBootRole(4L,"ROLE_ANONYMOUS", "游客"));
        qBootUserDetails.setRoles(roles);
        qBootUserDetails.setUserName(user.getUserName());
        qBootUserDetails.setEnabled(user.getEnabled());
        qBootUserDetails.setLocked(user.getLocked());
        qBootUserDetails.setPassword(user.getPassword());
        qBootUserDetails.setUpdatedTime(user.getUpdatedTime());
        qBootUserDetails.setRole_users(roles);

        qBootUserDetails.setEMail(user.getEMail());
        return qBootUserDetails;
    }
}
